<?php


namespace app\common\model;

use think\facade\Db;

class BaseModel
{
    protected $name = '';
    protected $id = '';

    public function pageData($option, $field = '', $where = [])
    {
        if (isset($option["keywords"]) && $option["keywords"] && $field) {
            $where[] = [$field, "like", "%" . $option["keywords"] . "%"];
        }
        $limit = isset($option['limit ']) ? $option["limit"] : 15;
        $data = [];
        $data["list"] = Db::table($this->name)
            ->where($where)
            ->page($option['page'], $limit)
            ->select();
        $total = Db::table($this->name)
            ->where($where)
            ->count();
        $data["pageTotal"] = $total;
        return $data;
    }

    public function saveData($data, $isCreate = true)
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        if ($isCreate) {
            $data["creat_time"] = date("Y-m-d H:i:s");
        }
        $insertId = Db::table($this->name)->insertGetId($data);
        if ($insertId) {
            $result["status"] = 'ok';
            $result["msg"] = "添加成功";
        }
        return $result;
    }

    public function editData($id, $data)
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $check = Db::table($this->name)->where($this->id, $id)->find();
        if (empty($check)) {
            return $result;
        }
        $updateResult = Db::table($this->name)->where($this->id, $id)->update($data);
        if ($updateResult) {
            $result["status"] = 'ok';
            $result["msg"] = "更新成功";
        } else {
            $result["msg"] = "请确认更新数据 ";
        }
        return $result;
    }

}